package com.zytlifeservice.controller;

import com.zytlifeservice.domain.Result;
import com.zytlifeservice.domain.User;
import com.zytlifeservice.repository.UserRepository;
import com.zytlifeservice.service.UserService;
import com.zytlifeservice.service.find.IntegralOrderService;
import com.zytlifeservice.util.ResultUtil;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by MJS on 2017/4/22.
 */

@RestController
@RequestMapping("/user")
public class UserController {
    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    @Autowired
    private IntegralOrderService integralOrderService;//支付订单对象

    /*
    * 注册
    * */
    @PostMapping(value = "/registerUser")
    public Result addUser(@RequestParam("mobile") String mobile,
                          @RequestParam("password") String password,
                          @RequestParam("area") String area,
                          @RequestParam("verify") String verify) {

        User user = new User();
        user.setMobile(mobile);
        user.setPassword(password);
        user.setArea(area);
        return ResultUtil.success(userService.add(user, verify), "注册成功");
    }

    /**
     * 登录
     */
    @PostMapping(value = "/login")
    public Result login(@RequestParam("mobile") String mobile,
                        @RequestParam("password") String password) {
        return ResultUtil.success(userService.login(mobile, password), "登录成功");
    }

    /**
     * 修改个人资料  + 和传入的手机号码的匹配。
     */
    @PostMapping(value = "/update/user")
    public Result update(User user, @RequestParam("verify") String verify) {
        userService.update(user, verify);
        return ResultUtil.success();
    }

    /**
     * 忘记密码
     */
    @PostMapping(value = "/forgetPassword")
    public Result forgetPassword(@RequestParam("mobile") String mobile,
                                 @RequestParam("verify") String verify,
                                 @RequestParam("password") String password) {
        userService.forgetPassword(mobile, verify, password);
        return ResultUtil.success();
    }

    /**
     * 修改密码
     */
    @PostMapping(value = "/update/password")
    public Result updatePassword(@RequestParam("newPassword") String newPassword,
                                 @RequestParam("oldPassword") String oldPassword,
                                 @RequestParam("verify") String verify,
                                 @RequestParam("mobile") String mobile) {
        userService.updatePassword(mobile, oldPassword, newPassword, verify);
        return ResultUtil.success();
    }

    /**
     * 修改头像
     */
    @PostMapping(value = "/update/headImg")
    public Result updateHeadIcon(@RequestParam("userId") long userId,
                                 @RequestParam("headImg") String headImg) {
        //前期不另做文件服务器
        userService.updateHeadImg(userId, headImg);
        return ResultUtil.success();
    }

    /*
    查询用户信息
     */
    @PostMapping(value = "/query")
    public Result queryUserInfo(@RequestParam("userId") long userId) {
        return ResultUtil.success(userService.queryUserInfo(userId), "查询成功");
    }

    //查询用户信息
    @RequestMapping(value = "/findUser")
    public Result findUser(Long userId) {
        System.out.println("进入查询用户信息");
        JSONObject jsonObject = new JSONObject();
        User user = userService.findOddUser(userId);
        Map<String, Object> userMap = new HashMap<String, Object>();
        if (null != user) {
            if (null != user.getAddress()) {
                userMap.put("name", user.getName());
                userMap.put("moblie", user.getMobile());
                userMap.put("address", user.getAddress());
                return ResultUtil.success(userMap, "成功");
            } else {
                return ResultUtil.error(-2, "地址为空");
            }
        } else {
            return ResultUtil.error(-1, "未获取用户信息");
        }
    }

    //确认已经收货删除订单
    @RequestMapping(value = "deleteOrde1r")
    public Result deleteUserOrder(Long id) {
        System.out.println("进入用户删除订单方法");
        Integer result = integralOrderService.deleteIntegralOrder(id);
        if (result > 0) {
            return ResultUtil.success(null, "成功");
        } else {
            return ResultUtil.error(-1, "失败");
        }
    }
}